package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.entity.TblUser;
import com.example.mapper.TblUserMapper;
import com.example.pojo.RespInfo;
import com.example.utils.RedisUtil;
import com.fasterxml.jackson.databind.util.JSONPObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2019/8/29 11:34
 */
@Controller
@RequestMapping(value = "/test")
@Slf4j
public class TestController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TblUserMapper tblUserMapper;


    @PostMapping(value = "/testRedis")
    @ResponseBody
    public String TestRedis(String key, String value) {

        boolean set = redisUtil.set(key, value);
        if (set) {
            return "success";
        }
        return "error";
    }


    @PostMapping(value = "/testInsert")
    @ResponseBody
    public String TestInsert(String username, String password) {
        TblUser tblUser = new TblUser();
        tblUser.setUsername(username).setPassword(password);

        int insert = tblUserMapper.insert(tblUser);
        if (insert > 0) {
            return "success";
        }
        return "error";

    }

    @RequestMapping(value = "/testLogin")
    @ResponseBody
    public RespInfo testLogin(String username, String password) {
        //log.info("测试登陆 --> " + jsonObject);
        log.info("测试登陆 username --> " + username + " password -->  " + password);
        RespInfo respInfo = new RespInfo();
//        String username = jsonObject.getString("username");
//        String password = jsonObject.getString("password");
        try {
            if ("admin".equals(username) && "admin".equals(password)) {
                JSONObject json = new JSONObject();
                String token = UUID.randomUUID().toString().replace("-", "");
                json.put("token", token);
                return respInfo.setCode(0).setMessage("登陆成功").setData(json);
            } else {
                return respInfo.setCode(1).setMessage("登陆失败,用户名或密码错误").setData("");
            }
        } catch (Exception e) {
            e.printStackTrace();
            respInfo.setCode(-1).setMessage("登陆异常").setData("");
        }
        return respInfo;
    }


    public static void main(String[] args) {

        String s1 = "2019-09-05";
        String s2 = "2019-09-01";
        int i = s1.compareTo(s2);
        System.out.println(i);


    }


}
